package com.syncomponents.core;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import commonj.sdo.helper.XSDHelper;

/**
 * 加载SDO xsd定义文件
 * 
 * @author sunkey
 * @date Sep 3, 2013
 */
public class RegisterSDOXsd extends PathMatchingResourcePatternResolver implements InitializingBean {

    private List<String> locationPatterns = new ArrayList<String>();

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 注册SDO xsd
     * 
     * @throws IOException
     */
    public void register() throws IOException {

        for (String xsdPath : locationPatterns) {
            logger.info("loading {}", xsdPath);

            Resource[] resources = this.getResources(xsdPath);
            for (Resource resource : resources) {
                InputStream in = resource.getInputStream();
                XSDHelper.INSTANCE.define(in, null);
            }

        }
    }

    public void setLocationPatterns(List<String> locationPatterns) {
        this.locationPatterns = locationPatterns;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        register();
    }

}
